<?php

Yii::import('Cms.models.FbMetadata');
class FacebookTagContentController extends ContentBaseController implements ICmsContent
{    
    public $info=array(
        'name'=>'Facebook Tag',
        'description'=>'Facebook Metadata content',
        'path'=>'Cms/facebookTagContent',
        'model'=>'Cms.models.FbMetadata',
    );
    
    public function getDefaultConfig()
    {
        return CMap::mergeArray(parent::getDefaultConfig(), array(
        ));
    }
    
    public function actionInstall()
    {
        $this->installContent();
    }
    
    public function actionUninstall()
    {
        $this->uninstallContent();
    }
    
    public function actionIndex()
    {
        $this->render('/facebookTagContent/view',array(
            'model'=>FSM::run('Cms.FbMetadata.get', array('id'=>Yii::app()->request->getParam('id', 0)))->model,
        ));
    }
    
    public function actionAdmin()
    {
        $model=new FbMetadata('search');
        $model->unsetAttributes();  // clear any default values
        if(isset($_GET['FbMetadata']))
            $model->attributes=$_GET['FbMetadata'];

        $this->render('/facebookTagContent/admin',array(
            'model'=>$model,
        ));
    }
    
    public function actionCreate()
    {
        $this->actionUpdate();
    }
    
    public function actionUpdate()
    {
        if (Yii::app()->request->IsPostRequest) {
            // save posted data
            $_POST['validateOnly'] = ($this->post('ajax','') == 'fb-metadata-form');
            $result = FSM::run('Cms.FbMetadata.save', $_POST);
            $model = $result->model; 

            if ($this->post('ajax','') == 'fb-metadata-form'){
                echo $result->getActiveErrorMessages($result->model);
                Yii::app()->end();
            }   
            if (! $result->hasErrors())
            {
                $this->message = Yii::t('Core','Item has been saved successfully.');
                $params = array('id' => $model->id);
                if (isset($_GET['layout']))
                    $params['layout'] = $_GET['layout'];
                $this->redirect($this->createUrl('update',$params));
            }
        } else {
            // show edit form
            $id = $this->get('id', 0);
            if ($id == 0) {
                $model = new Image();
            } else {
                $model = FSM::run('Cms.FbMetadata.get', array('id' => $id))->model;
            }
        }
            
        $this->render('/facebookTagContent/update', array('model' => $model));
    }
    
    public function actionDelete()
    {
        if(Yii::app()->request->isPostRequest)
        {
            // we only allow deletion via POST request
            if (($id = $this->get('id',null)) !== null) {
                $ids = is_numeric($id) ? array($id) : explode(',',$id);
                
                // delete one or multiple objects given the list of object IDs
                $result = FSM::run('Cms.FbMetadata.delete', array('ids' => $ids));
                if ($result->hasErrors()) {
                    echo $result->getError('ErrorCode');
                } elseif(!Yii::app()->request->isAjaxRequest) {
                    // only redirect user to the admin page if it is not an AJAX request
                    $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
                }
            } else {
                throw new CHttpException(400,Yii::t('Core','Cannot delete item with the given ID.'));
            }
        } else {
            throw new CHttpException(400,Yii::t('Core','Invalid request. Please do not repeat this request again.'));
        }
    }
}